/*
 * Created on 4 mai 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.broadvision.bvgs.multilang.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionMapping;

import com.broadvision.data.client.Content;
import com.broadvision.data.client.DataManager;
import com.broadvision.data.common.ContentKey;
import com.broadvision.data.exceptions.ContentNotFoundException;
import com.broadvision.data.exceptions.DataDeadLockedException;
import com.broadvision.data.exceptions.DataException;
import com.broadvision.portal.services.Channel;
import com.broadvision.portal.services.PortalContentKeyNotFoundException;
import com.broadvision.portal.services.PortalDataException;
import com.broadvision.portal.services.PortalVisitor;
import com.broadvision.portal.services.Program;
import com.broadvision.portal.services.util.TransferBeanUtils;
import com.broadvision.portal.tools.action.channel.ChannelUtils;
import com.broadvision.portal.tools.action.program.ProgramUtils;
import com.broadvision.system.accesscontrol.client.AccessManager;
import com.broadvision.web.util.ViewDynaBean;

/**
 * @author hmartin
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ProgramMultilangViewAction extends
		IMultilangValueObjectMultilangViewAction {

	
    protected void prepareViewBeans(ActionMapping mapping, HttpServletRequest request, int serviceId, PortalVisitor portalVisitor, Content content)
    throws Exception
{
    super.prepareViewBeans(mapping, request, serviceId, portalVisitor, content);
    prepareContext(mapping, request);
    Program program = new Program(content.getServiceId(), content.getOid(), portalVisitor);
    Channel channel = getChannelValue(request);
    ViewDynaBean view = prepareProgramBean(mapping, channel, program);
    request.setAttribute(VIEW_BEAN_NAME, view);
    AccessManager accessMgr = AccessManager.instance();
    com.broadvision.visitor.client.Visitor visitor = portalVisitor.getVisitor();
    boolean isChannelAdmin = ChannelUtils.isChannelAdmin(serviceId, visitor);
    if(isChannelAdmin || accessMgr.isPermitted(visitor, program, "Manage"))
        PropertyUtils.setSimpleProperty(view, "editable", Boolean.TRUE);
    Map contextParams = prepareContextParams(request);
    request.setAttribute("pagetabParam", contextParams);
}

    protected ViewDynaBean prepareProgramBean(ActionMapping mapping, Channel channel, Program program)
    throws Exception
{
    ViewDynaBean view = new ViewDynaBean(VIEW_BEAN_NAME, mapping);
    TransferBeanUtils.copyPropertiesToView(view, program);
    view.set("channelId", new Long(channel.getId()));
    return view;
}
    protected Channel getChannelValue(HttpServletRequest request)
    throws Exception
{
    int serviceId = getServiceId(request);
    PortalVisitor portalVisitor = getPortalVisitor(request);
    String parameter = request.getParameter("channelId");
    long channelId = Long.parseLong(parameter);
    Channel channel = new Channel(serviceId, channelId, portalVisitor);
    return channel;
}

	
	/* (non-Javadoc)
	 * @see com.broadvision.bvgs.multilang.action.IMultilangValueObjectMultilangViewAction#getContentToTranslate(javax.servlet.http.HttpServletRequest)
	 */
	
	protected Content getContentToTranslate(
			HttpServletRequest request) {
        int serviceId = getServiceId(request);
        try {
			PortalVisitor portalVisitor = getPortalVisitor(request);
			String s = request.getParameter("programId");
			long programId = s != null ? Long.parseLong(s) : 0L;
			Program program = new Program(serviceId, programId, portalVisitor);
			ContentKey contentKey = new ContentKey(301,serviceId,program.getName());
			DataManager dataMgr = new DataManager();
			Content content = dataMgr.getContent(contentKey);
			return content;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PortalContentKeyNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DataDeadLockedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ContentNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PortalDataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DataException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
    protected void prepareContext(ActionMapping mapping, HttpServletRequest request)
    {
        String ctxKey = request.getParameter("ctx.f");
        request.setAttribute("ctx.f", ctxKey);
    }

    protected Map prepareContextParams(HttpServletRequest request)
    {
        Map map = new HashMap();
        for(int index = 0; index < PARAM_NAMES.length; index++)
        {
            String name = PARAM_NAMES[index];
            map.put(name, request.getParameter(name));
        }

        return map;
    }

	
	   protected String getTranslationTableName(){
		return TRANSLATION_TABLE_NAME;
	    }
	    protected String getTranslationLanguageFieldName(){
	    	return TRANSLATION_LANGUAGE_FIELD_NAME;
	    }
	    protected String getTranslationNameFieldName(){
	    	return TRANSLATION_NAME_FIELD_NAME;
	    }

	    public static String VIEW_BEAN_NAME = "epProgramView";
	    public static String PARAM_NAMES[] = {
	        "programId", "channelId", "ctx.f"
	    };
	    public static String TRANSLATION_TABLE_NAME = "KM_PROGRAM_TRANSLATE";
	    public static String TRANSLATION_LANGUAGE_FIELD_NAME = "KM_PROGRAM_LANGUAGE";
	    public static String TRANSLATION_NAME_FIELD_NAME = "PROGRAM_NAME_TRANSLATE";

}
